package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.RefundProductBillDetailMapper;
import com.canaan.business.domain.bill.RefundProductBillDetail;
import com.canaan.business.service.bill.IRefundProductBillDetailService;

/**
 * 退料单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Service
public class RefundProductBillDetailServiceImpl implements IRefundProductBillDetailService {
    @Resource
    private RefundProductBillDetailMapper refundProductBillDetailMapper;
    @Resource
    private IWmsProductService wmsProductService;

    /**
     * 查询退料单明细
     *
     * @param id 退料单明细主键
     * @return 退料单明细
     */
    @Override
    public RefundProductBillDetail selectRefundProductBillDetailById(Long id) {
        return refundProductBillDetailMapper.selectRefundProductBillDetailById(id);
    }

    /**
     * 查询退料单明细列表
     *
     * @param refundProductBillDetail 退料单明细
     * @return 退料单明细
     */
    @Override
    public List<RefundProductBillDetail> selectRefundProductBillDetailList(RefundProductBillDetail refundProductBillDetail) {
        return refundProductBillDetailMapper.selectRefundProductBillDetailList(refundProductBillDetail);
    }

    /**
     * 新增退料单明细
     *
     * @param refundProductBillDetail 退料单明细
     * @return 结果
     */
    @Override
    public int insertRefundProductBillDetail(RefundProductBillDetail refundProductBillDetail) {
        if(StringUtils.isBlank(refundProductBillDetail.getNo())){
            refundProductBillDetail.setNo(TableNoUtil.getNo(RefundProductBillDetail.class));
        }
        refundProductBillDetail.setCreateBy(SecurityUtils.getUsername());
        refundProductBillDetail.setCreateTime(DateUtils.getNowDate());
        return refundProductBillDetailMapper.insertRefundProductBillDetail(refundProductBillDetail);
    }

    /**
     * 批量新增退料单明细
     *
     * @param refundProductBillDetailList 退料单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertRefundProductBillDetail(List<RefundProductBillDetail> refundProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (RefundProductBillDetail refundProductBillDetail : refundProductBillDetailList){
            if(StringUtils.isBlank(refundProductBillDetail.getNo())){
                refundProductBillDetail.setNo(TableNoUtil.getNo(RefundProductBillDetail.class));
            }
            refundProductBillDetail.setCreateBy(username);
            refundProductBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<refundProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > refundProductBillDetailList.size()) {
                endIndex = refundProductBillDetailList.size();
            }
            insertNum = insertNum + refundProductBillDetailMapper.batchInsertRefundProductBillDetail(refundProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改退料单明细
     *
     * @param refundProductBillDetail 退料单明细
     * @return 结果
     */
    @Override
    public int updateRefundProductBillDetail(RefundProductBillDetail refundProductBillDetail) {
        refundProductBillDetail.setUpdateBy(SecurityUtils.getUsername());
        refundProductBillDetail.setUpdateTime(DateUtils.getNowDate());
        return refundProductBillDetailMapper.updateRefundProductBillDetail(refundProductBillDetail);
    }

    /**
     * 批量修改退料单明细
     *
     * @param refundProductBillDetailList 退料单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateRefundProductBillDetail(List<RefundProductBillDetail> refundProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (RefundProductBillDetail refundProductBillDetail : refundProductBillDetailList){
            refundProductBillDetail.setUpdateBy(username);
            refundProductBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<refundProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > refundProductBillDetailList.size()) {
                endIndex = refundProductBillDetailList.size();
            }
            updateNum = updateNum + refundProductBillDetailMapper.batchUpdateRefundProductBillDetail(refundProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除退料单明细
     *
     * @param ids 需要删除的退料单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteRefundProductBillDetailByIds(Long[] ids) {
        return refundProductBillDetailMapper.deleteRefundProductBillDetailByIds(ids);
    }

    /**
     * 删除退料单明细信息
     *
     * @param id 退料单明细主键
     * @return 结果
     */
    @Override
    public int deleteRefundProductBillDetailById(Long id) {
        return refundProductBillDetailMapper.deleteRefundProductBillDetailById(id);
    }

    /**
     * 查询退料单明细
     *
     * @param no 编号
     * @return 退料单明细
     */
    @Override
    public RefundProductBillDetail selectRefundProductBillDetailByNo(String no) {
        return refundProductBillDetailMapper.selectRefundProductBillDetailByNo(no);
    }

    /**
     * 批量删除退料单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteRefundProductBillDetailByNos(String[] nos) {
        return refundProductBillDetailMapper.deleteRefundProductBillDetailByNos(nos);
    }

    /**
     * 删除退料单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteRefundProductBillDetailByNo(String no) {
        return refundProductBillDetailMapper.deleteRefundProductBillDetailByNo(no);
    }
}
